// pages/lstBaoQuan/lstBaoQuan.js
import request from '../../utils/config.js';
import util from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    preservStartDate:"",
    preservEndDate:"",
    det: "",
    flag: "",
    caseId: ""
  },

  //日期绑定
  startDateChange: function (e) {
    this.setData({
      preservStartDate: e.detail.value
    })
  },

  //日期绑定
  endDateChange: function (e) {
    this.setData({
      preservEndDate: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let info = JSON.parse(options.dataInfo);
    let defaultDet = info.det;
    if (info.flag == "add") {
      wx.setNavigationBarTitle({
        title: '添加案件财保',
      })
    } else if (info.flag == "edit") {
      wx.setNavigationBarTitle({
        title: '编辑案件财保',
      })
    }
    
    if (!defaultDet) {
      let _date = new Date();
      let Y = _date.getFullYear() + '-' ;
      var M = (_date.getMonth() + 1 < 10 ? '0' + (_date.getMonth() + 1) : _date.getMonth() + 1) + '-';
      var D = (_date.getDate() < 10 ? '0' + (_date.getDate()) : _date.getDate()) + ' ';
      defaultDet = {
        preservTarget:'',
        guaranteeMethod:'',
        preservStartDate: Y + M + D,
        preservEndDate: Y + M + D
      }
    }
    this.setData({
      det: defaultDet,
      flag: info.flag,
      preservStartDate: defaultDet.preservStartDate,
      preservEndDate: defaultDet.preservEndDate,
      caseId: info.caseId
    });
  },


  //提交表单
  formSubmit: function (e) {
    let bodyInfo = e.detail.value;
    bodyInfo.preservStartDate = bodyInfo.preservStartDate.replace(/-/g, "");
    bodyInfo.preservEndDate = bodyInfo.preservEndDate.replace(/-/g, "");
    bodyInfo.caseId = this.data.caseId;

    let method = "";
    if (this.data.flag == "add") {
      method = "POST";
    } else if (this.data.flag == 'edit') {
      method = "PUT";
      bodyInfo.id = this.data.det.id
    }
    console.log("bodyInfo===", bodyInfo)

    wx.showLoading({ title: '加载中' })
    request.requestMisApi(method, "awsuitPreservInfos", bodyInfo, (res) => {
      console.log(res);
      wx.hideLoading()
      if (res.code == '00') {
        wx.showToast({ title: res.msg, icon: 'success' })
        wx.navigateBack();
      } else {
        wx.showToast({ title: res.msg, icon: 'none' })
      }
    }, (res) => {
      wx.hideLoading()
      console.log("接口错误=", res)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})